package com.price.common.base

import android.content.Context
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.price.common.extend.addNavigationBarHeightToMarginBottom
import com.price.common.extend.appLightMode
import com.price.common.extend.attachLanguage
import com.price.common.extend.contentView
import com.price.common.extend.doCustomCrashListener
import com.price.common.extend.immerseStatusBar
import kotlin.jvm.Throws


abstract class BaseActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        runCatching {
            val viewId = getViewId()
            if (viewId != 0) {
                setContentView(getViewId())
            }
            immerseStatusBar(appLightMode)

            if(navMarginBottom()){
                contentView.addNavigationBarHeightToMarginBottom()
            }

            initView(savedInstanceState)
            initData(savedInstanceState)
        }.onFailure {e->
            doCustomCrashListener?.onCrash(e)
        }
    }

    abstract fun getViewId(): Int

    abstract fun initView(savedInstanceState: Bundle?)

    abstract fun initData(savedInstanceState: Bundle?)

    //是否在底部抬高虚拟按键高度
    open fun navMarginBottom():Boolean{
        return true
    }

    override fun attachBaseContext(newBase: Context?) {
        super.attachBaseContext(newBase?.attachLanguage())
    }

    override fun onResume() {
        super.onResume()
        runCatching {
            resume()
        }.onFailure {e->
            doCustomCrashListener?.onCrash(e)
        }
    }

    override fun onPause() {
        super.onPause()
        runCatching {
            pause()
        }.onFailure {e->
            doCustomCrashListener?.onCrash(e)
        }
    }

    open fun resume(){}
    open fun pause(){}
}